package com.sihan.swvp.common;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;

/**
 * MP4工具
 * @author jch
 *
 */
public class Mp4Util {

	/**
	 * 获取MP4时间长度
	 * @param path 路径
	 * @return
	 */
	public static long getDuration(String path) {

		return getDuration(new File(path));
	}

	/**
	 * 获取MP4时间长度
	 * @param file MP4
	 * @return
	 */
	public static long getDuration(File file) {
		try {
			Encoder encoder = new Encoder();
			MultimediaInfo m = encoder.getInfo(file);
			long ls = m.getDuration();
			
			return ls;
		} catch (Exception e) {

			return -1;
		}
	}
	
	/**
	 * 获取MP4图片
	 * @param path 路径
	 * @param time 时间
	 * @return
	 */
	public static byte[] getVideoPic(String path, double time){
		return getVideoPic(path, time, 0, 0);
	}

	/**
	 * 获取MP4图片
	 * @param path 路径
	 * @param time 时间
	 * @param width 截图高度
	 * @param height 截图宽度
	 * @return
	 */
	public static byte[] getVideoPic(String path, double time, int width,
			int height) {
		
		 String basePath= "";
		 try {
			 basePath = Mp4Util.class.getResource("/").toURI().getPath(); 
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		 File _temp = new File(basePath,"tools/"+UUID.randomUUID()+".jpg");
		 try {
			_temp.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
		 List<String> commend = new java.util.ArrayList<String>();  
        commend.add(new File(basePath,"tools/ffmpeg.exe").getAbsolutePath());  
        commend.add("-ss");  
        commend.add(CommonUtil.o2string(time));  
        commend.add("-i");  
        commend.add(path);  
        commend.add("-y");  
        commend.add("-f");  
        commend.add("image2");  
        commend.add("-t");  
        commend.add("0.001");  
        if(width > 0 && height > 0){
        	commend.add("-s");
            commend.add(width+"x"+height);
        }
        commend.add(_temp.getAbsolutePath());
        
        ProcessBuilder builder = new ProcessBuilder();  
        
        builder.command(commend);  
 
        builder.redirectErrorStream(true);  
 
        System.out.println("视频截图开始...");  
 
        byte[] data = null;
        InputStream in = null;
        FileInputStream fin = null;
		try {
			Process process = builder.start();
			in = process.getInputStream();  
	         byte[] re = new byte[1024];  
	         while (in.read(re) != -1) {}  
	  
	         fin = new FileInputStream(_temp);
	         data = new byte[fin.available()];
	         fin.read(data);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		finally{
			CommonUtil.close(in);
			CommonUtil.close(fin);
			CommonUtil.delete(_temp);
		}
         
		
		return data;
	}
	
	
	public static void copy(File source, File target) throws Exception {
		if(source.exists() && source.canRead()){
			if(target.exists() && target.length() == source.length()){
				return ;
			}
			
			if(!target.getParentFile().exists()) {
				target.getParentFile().mkdirs();
			}
			
			Files.copy(source.toPath(), target.toPath());
		}
	} 
	
	public static void delete(String path){
		delete(new File(path));
	}
	
	public static void delete(File file){
		if(file.exists()){
			file.delete();
		}
	}
}
